package com.atguigu.gmall.pms.service.impl;

import com.atguigu.gmall.pms.entity.AttrEntity;
import com.atguigu.gmall.pms.service.AttrService;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.gmall.common.bean.PageResultVo;
import com.atguigu.gmall.common.bean.PageParamVo;

import com.atguigu.gmall.pms.mapper.SpuAttrValueMapper;
import com.atguigu.gmall.pms.entity.SpuAttrValueEntity;
import com.atguigu.gmall.pms.service.SpuAttrValueService;


@Service("spuAttrValueService")
public class SpuAttrValueServiceImpl extends ServiceImpl<SpuAttrValueMapper, SpuAttrValueEntity> implements SpuAttrValueService {

    @Autowired
    private AttrService attrService;

    @Override
    public PageResultVo queryPage(PageParamVo paramVo) {
        IPage<SpuAttrValueEntity> page = this.page(
                paramVo.getPage(),
                new QueryWrapper<SpuAttrValueEntity>()
        );

        return new PageResultVo(page);
    }

    @Override
    public List<SpuAttrValueEntity> getSearchSpuAttrBySpuIdAndCId(Long spuId, Long cId) {
        QueryWrapper<AttrEntity> attrWrapper = new QueryWrapper<>();
        attrWrapper.eq("search_type",1).eq("type",1).eq("category_id",cId);
        List<AttrEntity> attrEntities = attrService.list(attrWrapper);

        if (Collections.isEmpty(attrEntities)){
            return null;
        }

        List<Long> longs = attrEntities.stream().map(AttrEntity::getId).collect(Collectors.toList());
        QueryWrapper<SpuAttrValueEntity> spuAttrWrapper = new QueryWrapper<>();
        spuAttrWrapper.eq("spu_id",spuId).in("attr_id",longs);
        List<SpuAttrValueEntity> list = this.list(spuAttrWrapper);
        if (Collections.isEmpty(list)){
            return null;
        }

        return list;
    }

}